package com.goicter.commons.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;

public class MyStringUtil {
	static Pattern pattern = Pattern.compile("[0-9]*");

	/**
	 * 把int型的數據字符串轉為integer數據集合
	 * 
	 * @param integersString
	 * @return
	 */
	public static List<Integer> stringToIntegerList(String integersString,
			String regex) {
		if (StringUtils.isNotEmpty(integersString)) {
			List<Integer> integerList = null;
			String[] integerStrArry = integersString.split(regex);
			if (integerStrArry != null && integerStrArry.length > 0) {
				integerList = new ArrayList<Integer>();
				for (int i = 0; i < integerStrArry.length; i++) {
					String str = integerStrArry[i].trim();
					if (StringUtils.isNotEmpty(str)
							&& pattern.matcher(str).matches()) {
						integerList.add(NumberUtils.toInt(str));
					}
				}
			}
			return integerList;
		}
		return null;
	}

	/**
	 * 獲取分表表名
	 * 
	 * @param tableInitName
	 * @param memberId
	 * @return
	 */
	public static String getTableNameByMemberId(String tableInitName,
			int memberId) {
		String memberIdStr = memberId + "";
		if (StringUtils.isEmpty(tableInitName) || memberIdStr.length() < 2) {
			return "";
		}
		String tableName = tableInitName
				+ "_"
				+ memberIdStr.substring(memberIdStr.length() - 2,
						memberIdStr.length());
		return tableName;
	}

	/**
	 * 根据接口名称获取接口错误编号
	 * 
	 * @param interfaceName
	 * @return
	 */
	public static int getInterfaceErrorCodeByInterfaceName(String interfaceName) {
		if (StringUtils.isEmpty(interfaceName)) {
			return 0;
		}
		return NumberUtils.toInt(PropertiesUtil.getUrl(
				"interface.error.code.properties", interfaceName));
	}

	/**
	 * 生成一个随机4位数字字符串
	 * 
	 * @return
	 */
	public static String getFourStr() {
		StringBuilder resultStr = new StringBuilder("");
		for (int i = 0; i < 4; i++) {
			int index = (int) (Math.random() * 10);
			resultStr.append(index);
		}
		return resultStr.toString();
	}

	/**
	 * 生成验证码
	 * 
	 * @param mobile
	 */
	public static String getVerifyCode(int verifyType, String mobile) {
		String verifyCode = null;
		switch (verifyType) {
		case 11:
			if (mobile.length() < 11) {
				return "0000";
			}
			verifyCode = String.valueOf((int) (Math.random() * 10));
			int mobileInt = NumberUtils.toInt(mobile.substring(3, 11));
			if (mobileInt == 0) {
				return "0000";
			}
			int index = ((mobileInt - verifyType) / verifyType + verifyType)
					% verifyType;
			String mobileIndex = String.valueOf(index);
			if (StringUtils.isEmpty(mobileIndex)) {
				return "0000";
			}
			int length = mobileIndex.length();
			if (length > 3) {
				mobileIndex = mobileIndex.substring(0, 3);
			}
			if (length < 3) {
				for (int i = 0; i < 3 - length; i++) {
					mobileIndex = mobileIndex + (i + 1);
				}
			}
			verifyCode = verifyCode + mobileIndex;
			break;
		default:
			return "0000";
		}
		return verifyCode;
	}

	/**
	 * 验证验证码
	 * 
	 * @param mobileVerifyCode
	 */
	public static boolean verifyVerifyCode(int verifyType, String verifyTarget,
			String verifyCode) {
		switch (verifyType) {
		case 11:
			if (StringUtils.isEmpty(verifyCode) || verifyCode.length() != 4) {
				return false;
			}
			String verifyMobileIndex = verifyCode.substring(1, 4);
			int mobileInt = NumberUtils.toInt(verifyTarget.substring(3, 11));
			if (mobileInt == 0) {
				return false;
			}
			int index = ((mobileInt - verifyType) / verifyType + verifyType)
					% verifyType;
			String mobileIndex = String.valueOf(index);
			if (StringUtils.isEmpty(mobileIndex)) {
				return false;
			}
			int length = mobileIndex.length();
			if (length > 3) {
				mobileIndex = mobileIndex.substring(0, 3);
			}
			if (length < 3) {
				for (int i = 0; i < 3 - length; i++) {
					mobileIndex = mobileIndex + (i + 1);
				}
			}
			if (mobileIndex.equals(verifyMobileIndex)) {
				return true;
			} else {
				return false;
			}
		default:
			return false;
		}
	}

	/**
	 * 通过地区描述获取语言编号
	 */
	public static int getLanguageByAreaStr(String areaStr) {
		if (StringUtils.isEmpty(areaStr)) {
			return 0;
		}
		int language = 0;
		if ("zh_CN".equalsIgnoreCase(areaStr)) {
			language = 1;
		} else if ("zh_TW".equalsIgnoreCase(areaStr)) {
			language = 0;
		} else if ("zh_HK".equalsIgnoreCase(areaStr)) {
			language = 0;
		} else if ("zh_MO".equalsIgnoreCase(areaStr)) {
			language = 0;
		} else {
			language = 0;
		}
		return language;
	}

	/**
	 * 生成验证码(改进版)
	 * 
	 * @param mobile
	 */
	public static String[] getVerifyCodeNew(int verifyType, String mobile) {
		String verifyCode = null;
		int defaultInt = (1 + (int) (Math.random() * 1000));
		Random rd = new Random(defaultInt);
		int randomInt = 1000 + rd.nextInt(7000);
		switch (verifyType) {
		case 11:
			if (mobile.length() < 11) {
				return new String[] { "0000", "1111" };
			}
			int mobileInt = NumberUtils.toInt(mobile.substring(3, 11));
			if (mobileInt == 0) {
				return new String[] { "0000", "1111" };
			}
			int index = ((mobileInt - verifyType) / verifyType + verifyType)
					% verifyType;
			String mobileIndex = String.valueOf(index);
			if (StringUtils.isEmpty(mobileIndex)) {
				return new String[] { "0000", "1111" };
			}
			int length = mobileIndex.length();
			if (length > 3) {
				mobileIndex = mobileIndex.substring(0, 3);
			}
			if (length < 3) {
				for (int i = 0; i < 3 - length; i++) {
					mobileIndex = mobileIndex + (i + 1);
				}
			}
			verifyCode = String.valueOf((randomInt | Integer
					.valueOf(1 + mobileIndex)) + 156);
			break;
		default:
			return new String[] { "0000", "1111" };
		}
		return new String[] { verifyCode, String.valueOf(randomInt) };
	}

	/**
	 * 验证验证码(改进版)
	 * 
	 * @param mobileVerifyCode
	 */
	public static boolean verifyVerifyCodeNew(int verifyType,
			String verifyTarget, int randomInt, String verifyCode) {
		switch (verifyType) {
		case 11:
			if (StringUtils.isEmpty(verifyCode) || verifyCode.length() != 4) {
				return false;
			}
			int mobileInt = NumberUtils.toInt(verifyTarget.substring(3, 11));
			if (mobileInt == 0) {
				return false;
			}
			int index = ((mobileInt - verifyType) / verifyType + verifyType)
					% verifyType;
			String mobileIndex = String.valueOf(index);
			if (StringUtils.isEmpty(mobileIndex)) {
				return false;
			}
			int length = mobileIndex.length();
			if (length > 3) {
				mobileIndex = mobileIndex.substring(0, 3);
			}
			if (length < 3) {
				for (int i = 0; i < 3 - length; i++) {
					mobileIndex = mobileIndex + (i + 1);
				}
			}
			String verifyCodeNew = String.valueOf((randomInt | Integer
					.valueOf(1 + mobileIndex)) + 156);

			if (verifyCodeNew.equals(verifyCode)) {
				return true;
			} else {
				return false;
			}
		default:
			return false;
		}
	}

	public static void main(String[] args) {
		String[] aa = getVerifyCodeNew(11, "18565678336");
		System.out.println(aa[0]);
		System.out.println(aa[1]);

		// System.out.println(verifyVerifyCode(11, "13823249393", "7712"));
	}
}
